from time import sleep
import log
import bitbrowser_api as api
import asyncio
from pyppeteer import *
import ai_api as ai

async def op_comment(ws_link):
    sleep(0.2)

    log.Info(f"Browser list:{ws_link}")

    try:

        browser = await connect({'browserWSEndpoint': ws_link})
        page = await browser.newPage()
        await page.setViewport({'width': 1366, 'height': 768})
        
        log.Info(f"开始处理{ws_link}")
        log.Info(f"打开页面")
        # 打开Facebook Marketplace销售页面
        await page.goto('https://www.facebook.com', {
            'waitUntil': 'networkidle2',
            'timeout': 60000
        })
        
        # 等待页面加载完成
        await page.waitForSelector('body')

        # 使用更精确的选择器定位点赞按钮
        opload_lable = await page.waitForXPath('(//span[contains(text(), "评论")])[3]', timeout=10000)
        await opload_lable.click()
        await asyncio.sleep(1)  

        await page.keyboard.type(ai.AI_chat("用英文生成一句心灵鸡汤,不要包含换行,并翻译成英文,30-100词左右"))

        await page.keyboard.press('Enter')

    except Exception as e:
        log.Error(f"评论失败: {str(e)}")


if __name__ == "__main__":
    ws_link = api.get_pyppeteer_wslink_by_ids(api.get_alive_ids()[0])
    asyncio.get_event_loop().run_until_complete(op_comment(ws_link))
        